package com.vf.admin.miaodong.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户
 */
@Repository
public class AfterUserDao extends BaseDao<AppUser> {

    /**
     * 详情
     */
    public AppUser get(Integer id) {
        if (id == null || id <= 0) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_user where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppUser.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppUser haoYou) {
        if (haoYou != null && haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 删除
     */
    public void del(Integer id) {
        if (id == null || id <= 0) {
            return;
        }
        String sql = "delete  from app_user where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }


    /**
     * 综合查询列表
     */
    public List<AppUser> findList(Map<String, Object> map, SplitPageBean bean) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append(" SELECT a.* FROM app_user a where 1=1 ");

        int size = map.size();

        if (StringEx.isNotNull(map.get("loginname"))) {
            sbStr.append(" and a.loginname like ? ");
            paramList.add("%" + map.get("loginname") + "%");
        }
        if (StringEx.isNotNull(map.get("code"))) {
            sbStr.append(" and a.code = ? ");
            paramList.add(map.get("code"));
        }
        if (StringEx.isNotNull(map.get("sex"))) {
            sbStr.append(" and a.sex = ? ");
            paramList.add(map.get("sex"));
        }
        if (StringEx.isNotNull(map.get("usercode"))) {
            sbStr.append(" and a.usercode = ? ");
            paramList.add(map.get("usercode"));
        }

        if (StringEx.isNotNull(map.get("states"))) {
            sbStr.append(" and a.states = ? ");
            paramList.add(map.get("states"));
        }
        if (StringEx.isNotNull(map.get("status"))) {
            sbStr.append(" and a.status = ? ");
            paramList.add(map.get("status"));
        }
        if (StringEx.isNotNull(map.get("nick"))) {
            sbStr.append(" and a.nick like ? ");
            paramList.add("%" + map.get("nick") + "%");
        }
        if (StringEx.isNotNull(map.get("startDate"))) {
            sbStr.append(" and DATE_FORMAT(a.createtime, '%Y-%m-%d')  >= ? ");
            paramList.add(map.get("startDate"));
        }
        if (StringEx.isNotNull(map.get("endDate"))) {
            sbStr.append(" and DATE_FORMAT(a.createtime, '%Y-%m-%d')  <= ? ");
            paramList.add(map.get("endDate"));
        }
        if (StringEx.isNotNull(map.get("isReal"))) {
            sbStr.append(" and a.isReal = ? ");
            paramList.add(map.get("isReal"));
        }
        if (StringEx.isNotNull(map.get("onlineStatus"))) {
            sbStr.append(" and a.onlinestatus = ? ");
            paramList.add(map.get("onlineStatus"));
        }
        if (StringEx.isNotNull(map.get("invitationcode"))) {
            sbStr.append(" and a.invitationcode = ? ");
            paramList.add(map.get("invitationcode"));
        }
        if (StringEx.isNotNull(map.get("windowTicket"))) {
            sbStr.append(" and a.windowTicket = ? ");
            paramList.add(map.get("windowTicket"));
        }
        if (StringEx.isNotNull(map.get("vipStatus"))) {
            sbStr.append(" and a.vip = ? ");
            paramList.add(map.get("vipStatus"));
        }

        if (StringEx.isNull(map.get("onlineStatus"))){
            sbStr.append(" and a.onlineStatus in(0,1,2) ");
        }

//        sbStr.append(" order by a.createtime desc,a.onlinestatus desc");
        sbStr.append(" order by a.onlinestatus asc,a.createtime desc");




        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppUser.class, bean);
    }

    public List<AppUser> findList1(Map<String, Object> map, SplitPageBean bean) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append(" SELECT a.* FROM agent_user_records r" +
                " left join app_user a " +
                " on r.userId=a.id" +
                " where r.parentUserId= " + map.get("parentUserId"));

        sbStr.append(" order by a.createtime desc ");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppUser.class, bean);
    }

    /**
     * 获取下级用户的总充值金额
     */
    public AppUser xiajiZongCzMoney(String code) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append(" SELECT sum(chongzhimoney) as chongzhimoney FROM app_user where 1=1 ");
        sbStr.append(" and invitationcode = ? ");
        paramList.add(code);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppUser.class);
    }

    /**
     * 根据邀请码查询用户
     *
     * @param code
     * @return
     */
    public AppUser getUserByCode(String code) {
        if (StringEx.isNull(code)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append(" select * from app_user where code = ?");
        paramList.add(code);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppUser.class);
    }

    /**
     * 账号状态修改
     *
     * @param id
     * @param state
     */
    public void upstate(Integer id, Integer state) {
        if (id == null || id <= 0 || state == null || state <= 0) {
            return;
        }
        String sql = " UPDATE app_user SET states = ?  WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(state);
        paramList.add(id);
        this.update(sql, paramList);
    }

    /**
     * 修改用户视频直播权限
     *
     * @param id
     * @param state
     */
    public void upstateVideoStatus(Integer id, Integer state) {
        if (id == null || id <= 0 || state == null || state <= 0) {
            return;
        }
        String sql = " UPDATE app_user SET shipinzb = ?  WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(state);
        paramList.add(id);
        this.update(sql, paramList);
    }

    /**
     * 修改用户实名认证状态
     *
     * @param id
     * @param state
     */
    public void upstateShiming(Integer id, Integer state) {
        if (id == null || id <= 0 || state == null || state <= 0) {
            return;
        }
        String sql = " UPDATE app_user SET status = ?  WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(state);
        paramList.add(id);
        this.update(sql, paramList);
    }

    /**
     * 修改用户实名认证状态
     *
     * @param id
     * @param state
     */
    public void upstateShipin(Integer id, Integer state) {
        if (id == null || id <= 0 || state == null || state <= 0) {
            return;
        }
        String sql = " UPDATE app_user SET shipinstate = ?  WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(state);
        paramList.add(id);
        this.update(sql, paramList);
    }

    /**
     * 修改用户语音直播权限
     *
     * @param id
     * @param state
     */
    public void upstateYuyinStatus(Integer id, Integer state) {
        if (id == null || id <= 0 || state == null || state <= 0) {
            return;
        }
        String sql = " UPDATE app_user SET yinpinzb = ?  WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(state);
        paramList.add(id);
        this.update(sql, paramList);
    }

    /*
     * @Description 更新用户金币
     * @Param: [id, jinbi]
     * @return: void
     * @Author: ljx
     * @Date: 2021/07/22
     */
    public void updatejinbi(Integer id, BigDecimal jinbi) {
        if (id == null || id <= 0 || jinbi == null) {
            return;
        }
        String sql = " UPDATE app_user SET jinbi = jinbi+ ?  WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(jinbi);
        paramList.add(id);
        this.update(sql, paramList);
    }

    /**
     * 减少用户金币
     *
     * @Param: [id, jinbi]
     * @return: void
     * @Author: zhouyunyi
     * @Date: 2021/07/28
     * @Time: 16:08
     */
    public void upJianjinbi(Integer id, BigDecimal jinbi) {
        if (id == null || id <= 0 || jinbi == null) {
            return;
        }
        String sql = " UPDATE app_user SET jinbi = jinbi- ?  WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(jinbi);
        paramList.add(id);
        this.update(sql, paramList);
    }

    /*
     * @Description 更新钻石数量
     * @Param: [id, zuanshi]
     * @return: void
     * @Author: ljx
     * @Date: 2021/07/24
     */
    public void updateZuanShi(Integer id, BigDecimal zuanshi) {
        if (id == null || id <= 0 || zuanshi == null || zuanshi.compareTo(BigDecimal.ZERO) <= 0) {
            return;
        }
        String sql = " UPDATE app_user SET diamonds = diamonds + ?  WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(zuanshi);
        paramList.add(id);
        this.update(sql, paramList);
    }

    /**
     * 减少钻石数量
     *
     * @Param: [id, zuanshi]
     * @return: void
     * @Author: zhouyunyi
     * @Date: 2021/07/28
     * @Time: 16:09
     */
    public void upJianZuanShi(Integer id, Integer zuanshi) {
        if (id == null || id <= 0 || zuanshi == null || zuanshi <= 0) {
            return;
        }
        String sql = " UPDATE app_user SET diamonds = diamonds - ?  WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(zuanshi);
        paramList.add(id);
        this.update(sql, paramList);
    }

    /*
     * @Description 更新魅力值数量
     * @Param: [id, meili]
     * @return: void
     * @Author: ljx
     * @Date: 2021/07/24
     */
    public void updatemeili(Integer id, Integer meili) {
        if (id == null || id <= 0 || meili == null || meili <= 0) {
            return;
        }
        String sql = " UPDATE app_user SET charmvalue = charmvalue + ?  WHERE id = ?  ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(meili);
        paramList.add(id);
        this.update(sql, paramList);
    }

    /**
     * 减少魅力值
     *
     * @Param: [id, meili]
     * @return: void
     * @Author: zhouyunyi
     * @Date: 2021/07/28
     * @Time: 16:09
     */
    public void upJianmeili(Integer id, Integer meili) {
        if (id == null || id <= 0 || meili == null || meili <= 0) {
            return;
        }
        String sql = " UPDATE app_user SET charmvalue = charmvalue - ?  WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(meili);
        paramList.add(id);
        this.update(sql, paramList);
    }

    public AppUser getByUserCodeOrGoodCodeExistUserId(Integer userId, Integer userCode) {
        String sql = " select * from app_user where id != ? and (usercode = ? or goodusercode = ?)";
        ArrayList<Object> paramList = new ArrayList<>();
        paramList.add(userId);
        paramList.add(userCode);
        paramList.add(userCode);
        return this.queryObj(sql, paramList, AppUser.class);
    }

    public void updateGoodUserCode(Integer userId, Integer goodUserCode) {

        String sql = " UPDATE app_user SET goodusercode = ? WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<>();
        paramList.add(goodUserCode);
        paramList.add(userId);
        this.update(sql, paramList);
    }

    public void upWhiteStatus(Integer id, Integer state) {

        String sql = " UPDATE app_user SET whiteStatus = ? WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<>();
        paramList.add(state);
        paramList.add(id);
        this.update(sql, paramList);
    }
}
